<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">

    
      <link rel="icon" href="/favicon.png" />
    

    <title>
        
          CODE&#39;NOTE
        
    </title>

    <!-- Spectre.css framework -->
    <link href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/spectre.css/0.5.9/spectre.min.css" rel="stylesheet">
    <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/spectre.css/0.5.9/spectre-exp.min.css" rel="stylesheet">
    <link href="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/spectre.css/0.5.9/spectre-icons.min.css" rel="stylesheet">

    <!-- theme css & js -->
    
<link rel="stylesheet" href="/css/book.css">

    
<script src="/js/book.js"></script>


    <!-- tocbot -->
    <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/tocbot/4.18.2/tocbot.min.js"></script>
    <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/tocbot/4.18.2/tocbot.css" rel="stylesheet">
    
    <!-- katex -->    
    <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/KaTeX/0.15.2/katex.min.css" rel="stylesheet">

    
    
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/zooming/2.1.1/zooming.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    const zooming = new Zooming()
    zooming.listen('.book-content img')
})
</script>

<meta name="generator" content="Hexo 6.1.0"></head>

<body>

<div class="book-container">
  <div class="book-sidebar">
    <div class="book-brand">
  <a href="/">
    <img src="/favicon.png">
    <span>CODE&#39;NOTE</span>
  </a>
</div>
    <div id="menu" class="book-menu hide">
  <h2 id="☘️-Home">☘️ <a href="/">Home</a></h2>
<h2 id="Go">Go</h2>
<ul>
<li><a href="/go/01%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA">0.1 开发环境搭建</a></li>
<li><a href="/go/02%E5%8F%98%E9%87%8F%E5%92%8C%E5%B8%B8%E9%87%8F">0.2 变量和常量</a></li>
<li><a href="/go/03%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B">0.3 基本数据类型</a></li>
<li><a href="/go/04%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6">0.4 流程控制和运算符</a></li>
<li><a href="/go/05%E6%95%B0%E7%BB%84">0.5 数组</a></li>
<li><a href="/go/06%E5%88%87%E7%89%87">0.6 切片</a></li>
<li><a href="/go/07map">0.7 map</a></li>
<li><a href="/go/08%E5%87%BD%E6%95%B0">0.8 函数</a></li>
<li><a href="/go/09%E6%8C%87%E9%92%88">0.9 指针</a></li>
<li><a href="/go/10%E5%8F%8D%E5%B0%84">1.0 反射</a></li>
<li><a href="/go/11%E7%BB%93%E6%9E%84%E4%BD%93">1.1 结构体</a></li>
<li><a href="/go/12%E6%8E%A5%E5%8F%A3">1.2 接口</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/goroutine">1.3 goroutine</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/channel">1.4 channel</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/%E5%B9%B6%E5%8F%91%E5%AE%89%E5%85%A8%E5%92%8C%E9%94%81">1.5 并发安全和锁</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/%E5%8E%9F%E5%AD%90%E6%93%8D%E4%BD%9C">1.6 原子操作</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/GPM%E5%8E%9F%E7%90%86%E4%B8%8E%E8%B0%83%E5%BA%A6">1.7 GPM与调度分析</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/CSP%E5%B9%B6%E5%8F%91%E6%A8%A1%E5%9E%8B">1.8 CSP并发模型</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/fmt">标准库fmt</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/flag">标准库flag</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/time">标准库time</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/go_%E6%A0%87%E5%87%86%E5%BA%93log">标准库log</a></li>
</ul>
<!-- ##
## .NET

* [C# 入门基础合集](/dotnet/基础合集)
* [C# 面向对象](/dotnet/面向对象)
* [C# 数据结构](/dotnet/数据结构)
* [C# 泛型技术](/dotnet/泛型技术)
* [C# 反射技术](/dotnet/反射技术)
* [C# 多线程开发](/dotnet/多线程开发)
* [C# 多线程之线程同步技术](/dotnet/多线程之线程同步技术)
* [C# 委托和事件详解](/dotnet/委托和事件详解)
* [C# 并行编程](/dotnet/并行编程)
* [C# 异步编程](/dotnet/异步编程)
* [C# 值类型和引用类型内存分配](/dotnet/值类型和引用类型的内存分配)
* [1.1 DataProtection简介](/dotnet/1.1DataProtection简介)
* [1.2 DataProtection方法介绍](/dotnet/1.2DataProtection方法介绍)
* [1.3 落地实践及多环境调试](/dotnet/1.3落地实践及多环境调试)
* [1.4 UserSecrets](/dotnet/1.4UserSecrets)
* [1.5 数据保护方案](/dotnet/1.5基于DataProtection的数据保护方案)
* [1.6 集成AzureDevops](/dotnet/1.6方案实践及集成AzureDevops管道)


ELK Stack

* [ElastaticSearch入门](/elk/1.1ElastaticSearch入门)
* [ElastaticSearch配置](/elk/1.2ElastaticSearch配置)
* [使用Kibana操作ES](/elk/1.3使用Kibana操作ES)
 ## Docker

* [1.1 Docker概念及安装](/docker/1.1Docker概念及安装)
* [1.2 Docker镜像](/docker/1.2Docker镜像)
* [1.3 Docker容器](/docker/1.3Docker容器)
* [1.4 Docker仓库](/docker/1.4Docker仓库)
* [1.5 Docker数据管理](/docker/1.5Docker数据管理)
* [1.6 Docker容器四种网络模式](/docker/1.6Docker容器四种网络模式)
* [1.7 Docker高级网络配置](/docker/1.7Docker高级网络配置)
* [1.8 Dockerfile](/docker/1.8Dockerfile)
* [1.9 DockerCompose](/docker/1.9Docker三剑客之DockerCompose)
* [2.0 DockerMachine](/docker/2.0Docker三剑客之DockerMachine)
* [2.1 DockerSwarm](/docker/2.1Docker三剑客之DockerSwarm)
* [2.2 Docker常用命令](/docker/2.2Docker常用命令)
* [Portainer可视化面板](/docker/Portainer可视化面板)
* [1.6 Docker四种网络模式](/docker/1.6Docker容器网络模式)
* [1.7 Docker高级网络配置](/docker/1.7Docker高级网络配置)
* [1.8 Dockerfile](/docker/1.8Dockerfile)
* [1.9 DockerCompose](/docker/1.9DockerCompose)
* [2.0 DockerMachine](/docker/2.0DockerMachine)
* [2.1 DockerSwarm](/docker/2.1DockerSwarm)
* [2.2 Docker常用命令](/docker/2.2Docker常用命令)
* [Portainer可视化面板](/docker/Portainer可视化面板) 

## Linux

* [Centos安装](/linux/Centos安装)
* [Linux目录说明](/linux/Linux目录说明)
* [Centos网络配置](/linux/Centos网络配置)
* [Centos7升级gcc版本](/linux/Centos7升级gcc版本)
* [Linux常用命令](/linux/Linux常用命令)

## RabbitMQ

* [1.1 RabbitMQ概念及安装](/rabbitMq/1.1RabbitMQ概念及安装)
* [1.2 工作模式介绍](/rabbitMq/1.2工作模式介绍)
* [1.3 消息确认及持久化](/rabbitMq/1.3消息确认及持久化)
* [1.4 两种消费模式和QOS](/rabbitMq/1.4两种消费模式和QOS的实现)
* [1.5 Channel常见方法](/rabbitMq/1.5Channel常见方法)
* [1.6 RabbitMQ常用命令](/rabbitMq/1.6RabbitMQ常用命令)
* [1.7 RabbitMQ常见策略](/rabbitMq/1.7RabbitMQ常见策略)
* [1.8 RabbitMQ常见问题](/rabbitMq/1.8RabbitMQ常见问题)
* [1.9 RabbitMQ集群方案](/rabbitMq/1.9RabbitMQ集群方案)
* [客户端连接RabbitMQ](/rabbitMq/客户端连接RabbitMQ)

## Redis

* [1.1 NoSql概述](/redis/1.1NoSql概述)
* [1.2 Redis安装](/redis/1.2Redis安装)
* [1.3 Redis基本数据类型](/redis/1.3Redis基本数据类型)
* [1.4 Redis特殊数据类型](/redis/1.4Redis特殊数据类型)
* [1.5 Redis事务操作](/redis/1.5Redis事务操作)
* [1.6 Redis配置文件详解](/redis/1.6Redis配置文件详解)
* [1.7 Redis持久化](/redis/1.7Redis持久化)
* [1.8 Redis发布订阅](/redis/1.8Redis发布订阅)
* [1.9 Redis集群方案](/redis/1.9Redis集群方案)
* [1.10 Redis常见问题](/redis/1.10Redis常见问题)
* [客户端连接Redis](/redis/客户端连接Redis)
* [使用Docker搭建Redis集群](/redis/使用Docker搭建Redis集群)

## MicroService

* [1.1 微服务之项目搭建](/microservice/1.1微服务入门之项目搭建)
* [1.2 微服务之服务注册发现](/microservice/1.2微服务入门之服务注册与发现)
* [1.3 微服务之网关](/microservice/1.3微服务入门之网关)
* [1.4 微服务之事件总线](/microservice/1.4微服务入门之事件总线)
* [1.5 微服务之DockerCompose](/microservice/1.5微服务入门之DockerCompose)
* [Consul服务注册发现](/microservice/Consul服务注册发现)

## Test

* [单元测试](/test/单元测试)
* [集成测试](/test/集成测试)
* [压力测试k6](/test/压力测试k6)
* [自动化UI测试](/test/自动化UI测试) -->

</div>


<script src="/js/book-menu.js"></script>

  </div>

  <div class="sidebar-toggle" onclick="sidebar_toggle()" onmouseover="add_inner()" onmouseleave="remove_inner()">
  <div class="sidebar-toggle-inner"></div>
</div>

<script>
function add_inner() {
  let inner = document.querySelector('.sidebar-toggle-inner')
  inner.classList.add('show')  
}

function remove_inner() {
  let inner = document.querySelector('.sidebar-toggle-inner')
  inner.classList.remove('show')
}

function sidebar_toggle() {
    let sidebar_toggle = document.querySelector('.sidebar-toggle')
    let sidebar = document.querySelector('.book-sidebar')
    let content = document.querySelector('.off-canvas-content')
    if (sidebar_toggle.classList.contains('extend')) { // show
        sidebar_toggle.classList.remove('extend')
        sidebar.classList.remove('hide')
        content.classList.remove('extend')
    }
    else { // hide
        sidebar_toggle.classList.add('extend')
        sidebar.classList.add('hide')
        content.classList.add('extend')
    }
}
</script>

  <div class="off-canvas-content">
    <div class="columns">
      <div class="column col-10 col-lg-12">
        <div class="book-navbar">
          <!-- For Responsive Layout -->

<header class="navbar">
  <section class="navbar-section">
    <a onclick="open_sidebar()">
      <i class="icon icon-menu"></i>
    </a>
  </section>
</header>

        </div>
        <div class="book-content">
          <div class="book-post">
  <p><code>fmt</code> 包实现了类似C#语言 <code>Console.Writeline</code> 和 <code>Console.ReadLine</code> 的格式化I/O。主要分为输出内容和获取输入两部分。</p>
<h1 id="输出">输出</h1>
<p>标准库 <code>fmt</code> 提供了以下几种输出相关函数。</p>
<h2 id="Print">Print</h2>
<p><code>Print </code> 系列函数会将内容输出到系统的标准输出，区别在于<code>Print</code>函数直接输出内容，<code>Printf</code>函数支持格式化输出字符串，<code>Println</code> 函数会在输出内容的结尾添加一个换行符。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Print</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Printf</span><span class="params">(format <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Println</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br></pre></td></tr></table></figure>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">fmtPrint</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Print(<span class="string">&quot;在终端打印该信息&quot;</span>)</span><br><span class="line">	name := <span class="string">&quot;wangpengliang&quot;</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;我是：%s\n&quot;</span>, name)</span><br><span class="line">	fmt.Println(<span class="string">&quot;在终端打印单独一行显示&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">在终端打印该信息我是：wangpengliang</span><br><span class="line">在终端打印单独一行显示</span><br></pre></td></tr></table></figure>
<h2 id="Fprint">Fprint</h2>
<p><code>Fprint</code> 系列函数会将内容输出到一个 <code>io.Writer</code> 接口类型的变量 <code>w</code> 中，通常用这个函数往文件中写入内容。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fprint</span><span class="params">(w io.Writer, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fprintf</span><span class="params">(w io.Writer, format <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fprintln</span><span class="params">(w io.Writer, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br></pre></td></tr></table></figure>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">fmtFprint</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="comment">// 向标准输出写入内容</span></span><br><span class="line">	fmt.Fprintln(os.Stdout, <span class="string">&quot;向标准输出写入内容&quot;</span>)</span><br><span class="line">	fileObj, err := os.OpenFile(<span class="string">&quot;./fprintTest.txt&quot;</span>, os.O_CREATE|os.O_WRONLY|os.O_APPEND, <span class="number">0644</span>)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		fmt.Println(<span class="string">&quot;打开文件出错，err:&quot;</span>, err)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	name := <span class="string">&quot;wangpengliang&quot;</span></span><br><span class="line">	<span class="comment">// 向打开的文件句柄中写入内容</span></span><br><span class="line">	fmt.Fprintf(fileObj, <span class="string">&quot;往文件中写入信息：%s&quot;</span>, name)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>：只要满足 <code>io.Writer</code> 接口的类型都支持写入。</p>
<h2 id="Sprint">Sprint</h2>
<p><code>Sprint</code>系列函数会把传入的数据生成并返回一个字符串。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sprint</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> <span class="type">string</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sprintf</span><span class="params">(format <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> <span class="type">string</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sprintln</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> <span class="type">string</span></span><br></pre></td></tr></table></figure>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">fmtSprint</span><span class="params">()</span></span> &#123;</span><br><span class="line">	s := <span class="string">&quot;wangpengliang&quot;</span></span><br><span class="line">	s1 := fmt.Sprint(s)</span><br><span class="line"></span><br><span class="line">	s2 := fmt.Sprintf(<span class="string">&quot;name:%s,age:%d&quot;</span>, <span class="string">&quot;wangpengliang&quot;</span>, <span class="number">18</span>)</span><br><span class="line"></span><br><span class="line">	fmt.Println(s1, s2)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="Errorf">Errorf</h2>
<p><code>Errorf</code> 函数根据 <code>format</code> 参数生成格式化字符串并返回一个包含该字符串的错误。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Errorf</span><span class="params">(format <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> <span class="type">error</span></span><br></pre></td></tr></table></figure>
<p>通常使用这种方式来自定义错误类型，例如：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">err := fmt.Errorf(<span class="string">&quot;这是一个错误&quot;</span>)</span><br></pre></td></tr></table></figure>
<p>Go1.13 版本为 <code>fmt.Errorf</code> 函数新加了一个<code>%w</code>占位符用来生成一个可以包裹 <code>Error</code> 的 <code>Wrapping Error</code> 。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">errorof</span><span class="params">()</span></span> &#123;</span><br><span class="line">	e := errors.New(<span class="string">&quot;原始错误e&quot;</span>)</span><br><span class="line">	w := fmt.Errorf(<span class="string">&quot;Wrap了一个错误%w&quot;</span>, e)</span><br><span class="line">	fmt.Println(w)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="格式化占位符">格式化占位符</h1>
<p><code>*printf</code> 系列函数都支持 <code>format</code> 格式化参数，这里按照占位符将被替换的变量类型划分，方便查询和记忆。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">%v，原样输出</span><br><span class="line">%T，打印类型</span><br><span class="line">%t，<span class="type">bool</span>类型</span><br><span class="line">%s，字符串</span><br><span class="line">%f，浮点</span><br><span class="line">%d，<span class="number">10</span>进制的整数</span><br><span class="line">%b，<span class="number">2</span>进制的整数</span><br><span class="line">%o，<span class="number">8</span>进制</span><br><span class="line">%x，%X，<span class="number">16</span>进制</span><br><span class="line">%x：<span class="number">0</span><span class="number">-9</span>，a-f</span><br><span class="line">%X：<span class="number">0</span><span class="number">-9</span>，A-F</span><br><span class="line">%c，打印字符</span><br><span class="line">%p，打印内存地址</span><br></pre></td></tr></table></figure>
<h2 id="通用占位符">通用占位符</h2>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>%v</code></td>
<td style="text-align:center">值的默认格式表示</td>
</tr>
<tr>
<td style="text-align:center"><code>%+v</code></td>
<td style="text-align:center">类似%v，但输出结构体时会添加字段名</td>
</tr>
<tr>
<td style="text-align:center"><code>%#v</code></td>
<td style="text-align:center">值的Go语法表示</td>
</tr>
<tr>
<td style="text-align:center"><code>%T</code></td>
<td style="text-align:center">打印值的类型</td>
</tr>
<tr>
<td style="text-align:center"><code>%%</code></td>
<td style="text-align:center">百分号</td>
</tr>
</tbody>
</table>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">generalPlaceholder</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%v\n&quot;</span>, <span class="number">100</span>)   <span class="comment">// %v：默认格式输出</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%v\n&quot;</span>, <span class="literal">false</span>) <span class="comment">// %v：默认格式输出</span></span><br><span class="line">	o := <span class="keyword">struct</span>&#123; name <span class="type">string</span> &#125;&#123;<span class="string">&quot;wangpengliang&quot;</span>&#125;</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%v\n&quot;</span>, o)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%+v\n&quot;</span>, o) <span class="comment">// %+v：类似%v，但输出结构体时会添加字段名</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%#v\n&quot;</span>, o) <span class="comment">// %#v：值的Go语法表示</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%T\n&quot;</span>, o)  <span class="comment">// %T：打印值的类型</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;100%%\n&quot;</span>)  <span class="comment">// %%：百分号</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">100</span><br><span class="line">false</span><br><span class="line">&#123;wangpengliang&#125;</span><br><span class="line">&#123;name:wangpengliang&#125;</span><br><span class="line">struct &#123; name string &#125;&#123;name:&quot;wangpengliang&quot;&#125;</span><br><span class="line">struct &#123; name string &#125;</span><br><span class="line"><span class="meta prompt_">100%</span></span><br></pre></td></tr></table></figure>
<h2 id="布尔型">布尔型</h2>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>%t</code></td>
<td style="text-align:center">true或false</td>
</tr>
</tbody>
</table>
<h2 id="整型">整型</h2>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>%b</code></td>
<td style="text-align:center">表示为二进制</td>
</tr>
<tr>
<td style="text-align:center"><code>%c</code></td>
<td style="text-align:center">该值对应的unicode码值</td>
</tr>
<tr>
<td style="text-align:center"><code>%d</code></td>
<td style="text-align:center">表示为十进制</td>
</tr>
<tr>
<td style="text-align:center"><code>%o</code></td>
<td style="text-align:center">表示为八进制</td>
</tr>
<tr>
<td style="text-align:center"><code>%x</code></td>
<td style="text-align:center">表示为十六进制，使用a-f</td>
</tr>
<tr>
<td style="text-align:center"><code>%X</code></td>
<td style="text-align:center">表示为十六进制，使用A-F</td>
</tr>
<tr>
<td style="text-align:center"><code>%U</code></td>
<td style="text-align:center">表示为Unicode格式：U+1234，等价于”U+%04X”</td>
</tr>
<tr>
<td style="text-align:center"><code>%q</code></td>
<td style="text-align:center">该值对应的单引号括起来的go语法字符字面值，必要时会采用安全的转义表示</td>
</tr>
</tbody>
</table>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">intPlaceholderTest</span><span class="params">()</span></span> &#123;</span><br><span class="line">	n := <span class="number">65</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%b\n&quot;</span>, n) <span class="comment">// %b：二进制输出</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%c\n&quot;</span>, n) <span class="comment">// %c：输出对应的unicode编码</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%d\n&quot;</span>, n) <span class="comment">// %d:十进制输出</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%o\n&quot;</span>, n) <span class="comment">// %o:八进制输出</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%x\n&quot;</span>, n) <span class="comment">// %x:十六进制输出,使用a-f</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%X\n&quot;</span>, n) <span class="comment">// %X:十六进制输出，使用A-F</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1000001</span><br><span class="line">A  </span><br><span class="line">65 </span><br><span class="line">101</span><br><span class="line">41 </span><br><span class="line">41</span><br></pre></td></tr></table></figure>
<h2 id="浮点数与复数">浮点数与复数</h2>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>%b</code></td>
<td style="text-align:center">无小数部分、二进制指数的科学计数法，如-123456p-78</td>
</tr>
<tr>
<td style="text-align:center"><code>%e</code></td>
<td style="text-align:center">科学计数法，如-1234.456e+78</td>
</tr>
<tr>
<td style="text-align:center"><code>%E</code></td>
<td style="text-align:center">科学计数法，如-1234.456E+78</td>
</tr>
<tr>
<td style="text-align:center"><code>%f</code></td>
<td style="text-align:center">有小数部分但无指数部分，如123.456</td>
</tr>
<tr>
<td style="text-align:center"><code>%F</code></td>
<td style="text-align:center">等价于%f</td>
</tr>
<tr>
<td style="text-align:center"><code>%g</code></td>
<td style="text-align:center">根据实际情况采用%e或%f格式（以获得更简洁、准确的输出）</td>
</tr>
<tr>
<td style="text-align:center"><code>%G</code></td>
<td style="text-align:center">根据实际情况采用%E或%F格式（以获得更简洁、准确的输出）</td>
</tr>
</tbody>
</table>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">floatPlaceholder</span><span class="params">()</span></span> &#123;</span><br><span class="line">	f := <span class="number">12.34</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%b\n&quot;</span>, f) <span class="comment">// %b：无小数部分、二进制指数的科学计数法</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%e\n&quot;</span>, f) <span class="comment">// %e: 科学计数法,如-1234.456e+78</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%E\n&quot;</span>, f) <span class="comment">// %E: 科学计数法,如-1234.456E+78</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%f\n&quot;</span>, f) <span class="comment">// %f: 有小数部分但无指数部分，如123.456</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%g\n&quot;</span>, f) <span class="comment">// %g: 根据实际情况采用%e或%f格式（以获得更简洁、准确的输出）</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%G\n&quot;</span>, f) <span class="comment">// %G: 根据实际情况采用%E或%F格式（以获得更简洁、准确的输出）</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">6946802425218990p-49</span><br><span class="line">1.234000e+01</span><br><span class="line">1.234000E+01</span><br><span class="line">12.340000   </span><br><span class="line">12.34       </span><br><span class="line">12.34</span><br></pre></td></tr></table></figure>
<h2 id="字符串和-byte">字符串和[]byte</h2>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>%s</code></td>
<td style="text-align:center">直接输出字符串或者[]byte</td>
</tr>
<tr>
<td style="text-align:center"><code>%q</code></td>
<td style="text-align:center">该值对应的双引号括起来的go语法字符串字面值，必要时会采用安全的转义表示</td>
</tr>
<tr>
<td style="text-align:center"><code>%x</code></td>
<td style="text-align:center">每个字节用两字符十六进制数表示（使用a-f)</td>
</tr>
<tr>
<td style="text-align:center"><code>%X</code></td>
<td style="text-align:center">每个字节用两字符十六进制数表示（使用A-F）</td>
</tr>
</tbody>
</table>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">stringPlaceholder</span><span class="params">()</span></span> &#123;</span><br><span class="line">	s := <span class="string">&quot;wangpengliang&quot;</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%s\n&quot;</span>, s) <span class="comment">// %s：直接输出字符串或者[]byte</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%q\n&quot;</span>, s) <span class="comment">// %q: 该值对应的双引号括起来的go语法字符串字面值，必要时会采用安全的转义表示</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%x\n&quot;</span>, s) <span class="comment">// %x: 每个字节用两字符十六进制数表示（使用a-f)</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%X\n&quot;</span>, s) <span class="comment">// %X: 每个字节用两字符十六进制数表示（使用A-F)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">wangpengliang</span><br><span class="line"><span class="string">&quot;wangpengliang&quot;</span></span><br><span class="line">77616e6770656e676c69616e67</span><br><span class="line">77616E6770656E676C69616E67</span><br></pre></td></tr></table></figure>
<h2 id="指针">指针</h2>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>%p</code></td>
<td style="text-align:center">表示为十六进制，并加上前导的<code>0x</code></td>
</tr>
</tbody>
</table>
<p>示例代码如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">pointerPlaceholder</span><span class="params">()</span></span> &#123;</span><br><span class="line">	a := <span class="number">10</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%p\n&quot;</span>, &amp;a)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%#p\n&quot;</span>, &amp;a)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">0xc000012088</span><br><span class="line">c000012088</span><br></pre></td></tr></table></figure>
<h2 id="宽度标识符">宽度标识符</h2>
<p>宽度通过一个紧跟在百分号后面的十进制数指定，如果未指定宽度，则表示值时除必需之外不作填充。精度通过（可选的）宽度后跟点号后跟的十进制数指定。如果未指定精度，会使用默认精度；如果点号后没有跟数字，表示精度为0。举例如下：</p>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>%f</code></td>
<td style="text-align:center">默认宽度，默认精度</td>
</tr>
<tr>
<td style="text-align:center"><code>%9f</code></td>
<td style="text-align:center">宽度9，默认精度</td>
</tr>
<tr>
<td style="text-align:center"><code>%.2f</code></td>
<td style="text-align:center">默认宽度，精度2</td>
</tr>
<tr>
<td style="text-align:center"><code>%9.2f</code></td>
<td style="text-align:center">宽度9，精度2</td>
</tr>
<tr>
<td style="text-align:center"><code>%9.f</code></td>
<td style="text-align:center">宽度9，精度0</td>
</tr>
</tbody>
</table>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">breadth</span><span class="params">()</span></span> &#123;</span><br><span class="line">	n := <span class="number">12.34</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%f\n&quot;</span>, n)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%9f\n&quot;</span>, n)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%.2f\n&quot;</span>, n)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%9.2f\n&quot;</span>, n)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%9.f\n&quot;</span>, n)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">12.340000</span><br><span class="line">12.340000</span><br><span class="line">12.34    </span><br><span class="line">    12.34</span><br><span class="line">       12</span><br></pre></td></tr></table></figure>
<h2 id="其他flag">其他flag</h2>
<table>
<thead>
<tr>
<th style="text-align:center">占位符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>’+’</code></td>
<td style="text-align:center">总是输出数值的正负号；对%q（%+q）会生成全部是ASCII字符的输出（通过转义）</td>
</tr>
<tr>
<td style="text-align:center"><code>’ ‘</code></td>
<td style="text-align:center">对数值，正数前加空格而负数前加负号；对字符串采用%x或%X时（% x或% X）会给各打印的字节之间加空格</td>
</tr>
<tr>
<td style="text-align:center"><code>’-’</code></td>
<td style="text-align:center">在输出右边填充空白而不是默认的左边（即从默认的右对齐切换为左对齐）</td>
</tr>
<tr>
<td style="text-align:center"><code>’#’</code></td>
<td style="text-align:center">八进制数前加0（%#o），十六进制数前加0x（%#x）或0X（%#X），指针去掉前面的0x（%#p）对%q（%#q），对%U（%#U）会输出空格和单引号括起来的go字面值</td>
</tr>
<tr>
<td style="text-align:center"><code>‘0’</code></td>
<td style="text-align:center">使用0而不是空格填充，对于数值类型会把填充的0放在正负号后面</td>
</tr>
</tbody>
</table>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">otherPlaceholder</span><span class="params">()</span></span> &#123;</span><br><span class="line">	s := <span class="string">&quot;wangpengliang&quot;</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%s\n&quot;</span>, s)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%5s\n&quot;</span>, s)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%-5s\n&quot;</span>, s)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%5.7s\n&quot;</span>, s)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%-5.7s\n&quot;</span>, s)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%5.2s\n&quot;</span>, s)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%05s\n&quot;</span>, s)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">wangpengliang</span><br><span class="line">wangpengliang</span><br><span class="line">wangpengliang</span><br><span class="line">wangpen      </span><br><span class="line">wangpen</span><br><span class="line">   wa</span><br><span class="line">wangpengliang</span><br></pre></td></tr></table></figure>
<h1 id="获取输入">获取输入</h1>
<p>Go语言<code>fmt</code>包下有<code>fmt.Scan</code>、<code>fmt.Scanf</code>、<code>fmt.Scanln</code>三个函数，可以在程序运行过程中从标准输入获取用户的输入。</p>
<h2 id="fmt-Scan">fmt.Scan</h2>
<p>函数签名如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Scan</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>Scan从标准输入扫描文本，读取由空白符分隔的值保存到传递给本函数的参数中，换行符视为空白符</li>
<li>本函数返回成功扫描的数据个数和遇到的任何错误。如果读取的数据个数比提供的参数少，会返回一个错误报告原因</li>
</ul>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">fmtScan</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Println(<span class="string">&quot;please input you name&quot;</span>)</span><br><span class="line">	<span class="keyword">var</span> input <span class="type">string</span></span><br><span class="line">	fmt.Scanln(&amp;input) <span class="comment">//读取键盘输入，通过操作地址赋值给input.阻塞式</span></span><br><span class="line">	fmt.Println(<span class="string">&quot;you name is：&quot;</span> + input)</span><br><span class="line"></span><br><span class="line">	fmt.Println(<span class="string">&quot;please input you name,age,sex&quot;</span>)</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name <span class="type">string</span></span><br><span class="line">		age  <span class="type">int</span></span><br><span class="line">		sex  <span class="type">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scan(&amp;name, &amp;age, &amp;sex)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;扫描结果 name:%s age:%d sex:%t \n&quot;</span>, name, age, sex)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">please input you name</span><br><span class="line">wangpengliang</span><br><span class="line">you name is：wangpengliang   </span><br><span class="line">please input you name,age,sex</span><br><span class="line">wangpengliang 18 <span class="literal">true</span></span><br><span class="line">扫描结果 name:wangpengliang age:18 sex:<span class="literal">true</span></span><br></pre></td></tr></table></figure>
<p><code>fmt.Scan</code> 从标准输入中扫描用户输入的数据，将以空白符分隔的数据分别存入指定的参数。</p>
<h2 id="fmt-Scanf">fmt.Scanf</h2>
<p>函数签名如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Scanf</span><span class="params">(format <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>Scanf从标准输入扫描文本，根据 <code>format</code> 参数指定的格式去读取由空白符分隔的值保存到传递给本函数的参数中</li>
<li>本函数返回成功扫描的数据个数和遇到的任何错误</li>
</ul>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">fmtScanf</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name <span class="type">string</span></span><br><span class="line">		age  <span class="type">int</span></span><br><span class="line">		sex  <span class="type">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scanf(<span class="string">&quot;1:%s 2:%d 3:%t&quot;</span>, &amp;name, &amp;age, &amp;sex)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;扫描结果 name:%s age:%d sex:%t \n&quot;</span>, name, age, sex)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>将上面的代码编译后在终端执行，在终端按照指定的格式依次输入<code>1:wpl 2:18 3:true</code>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">PS D:\GoProject\src\golang.basic&gt; go run .\fmtTest.go</span><br><span class="line">1:wpl 2:18 3:<span class="literal">true</span></span><br><span class="line">扫描结果 name:wpl age:18 sex:<span class="literal">true</span> </span><br></pre></td></tr></table></figure>
<p><code>fmt.Scanf</code> 不同于 <code>fmt.Scan</code> 简单的以空格作为输入数据的分隔符，<code>fmt.Scanf </code>为输入数据指定了具体的输入内容格式，只有按照格式输入数据才会被扫描并存入对应变量。</p>
<h2 id="fmt-Scanln">fmt.Scanln</h2>
<p>函数签名如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Scanln</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>Scanln类似Scan，它在遇到换行时才停止扫描。最后一个数据后面必须有换行或者到达结束位置</li>
<li>本函数返回成功扫描的数据个数和遇到的任何错误</li>
</ul>
<p>示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">fmtScanlnfTest</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name <span class="type">string</span></span><br><span class="line">		age  <span class="type">int</span></span><br><span class="line">		sex  <span class="type">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scanln(&amp;name, &amp;age, &amp;sex)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;扫描结果 name:%s age:%d sex:%t \n&quot;</span>, name, age, sex)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>将上面的代码编译后在终端执行，在终端依次输入<code>1</code>、<code>2</code>和<code>false</code>使用空格分隔。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">PS D:\GoProject\src\golang.basic&gt; go run .\fmtTest.go</span><br><span class="line">1 2 <span class="literal">false</span></span><br><span class="line">扫描结果 name:1 age:2 sex:<span class="literal">false</span></span><br></pre></td></tr></table></figure>
<p><code>fmt.Scanln</code> 遇到回车就结束扫描了，这个比较常用。</p>
<h2 id="bufio-NewReader">bufio.NewReader</h2>
<p>有时候需要完整获取输入的内容，而输入的内容可能包含空格，这种情况下可以使用 <code>bufio</code> 包来实现。比如：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">bufioTest</span><span class="params">()</span></span> &#123;</span><br><span class="line">	reader := bufio.NewReader(os.Stdin) <span class="comment">// 从标准输入生成读对象</span></span><br><span class="line">	fmt.Print(<span class="string">&quot;请输入内容：&quot;</span>)</span><br><span class="line">	text, _ := reader.ReadString(<span class="string">&#x27;\n&#x27;</span>) <span class="comment">// 读到换行</span></span><br><span class="line">	text = strings.TrimSpace(text)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;%#v\n&quot;</span>, text)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">请输入内容：1 2 3 43 435</span><br><span class="line">&quot;1 2 3 43 435&quot;</span><br></pre></td></tr></table></figure>
<h2 id="Fscan-系列">Fscan 系列</h2>
<p>这几个函数功能分别类似于 <code>fmt.Scan</code>、<code>fmt.Scanf</code>、<code>fmt.Scanln</code>三个函数，只不过它们不是从标准输入中读取数据而是从 <code>io.Reader</code> 中读取数据。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fscan</span><span class="params">(r io.Reader, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fscanln</span><span class="params">(r io.Reader, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Fscanf</span><span class="params">(r io.Reader, format <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br></pre></td></tr></table></figure>
<h2 id="Sscan-系列">Sscan 系列</h2>
<p>这几个函数功能分别类似于 <code>fmt.Scan</code> 、 <code>fmt.Scanf</code> 、<code>fmt.Scanln</code> 三个函数，只不过它们不是从标准输入中读取数据而是从指定字符串中读取数据。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sscan</span><span class="params">(str <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sscanln</span><span class="params">(str <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sscanf</span><span class="params">(str <span class="type">string</span>, format <span class="type">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span></span> (n <span class="type">int</span>, err <span class="type">error</span>)</span><br></pre></td></tr></table></figure>

</div>


  <div class="book-comments">
    




  </div>



<script src="/js/book-post.js"></script>

        </div>
      </div>
      <div class="column col-2 hide-lg">
        <div class="book-post-info">
  
    <div class="book-post-meta">

  <div class="author">

    <!-- Author image -->
    <div class="author-img">
      
        <figure
          class="avatar avatar-lg"
          data-initial="W"
          style="background-color: #3b4351;">
        </figure>
      
    </div>

    <!-- Author title -->
    <div class="author-title">
      <div>WangPengLiang</div>
      <div>2022-03-30</div>
    </div>
  </div>

  

  <div class="divider"></div>
</div>
  

  <div class="book-tocbot">
</div>
<div class="book-tocbot-menu">
  <a class="book-toc-expand" onclick="expand_toc()">Expand all</a>
  <a onclick="go_top()">Back to top</a>
  <a onclick="go_bottom()">Go to bottom</a>
</div>


<script src="/js/book-toc.js"></script>

</div>
      </div>
    </div>
  </div>
  
  <a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>

</body>
</html>


<script src="/js/book.js"></script>
